package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.view.ui.adapter.packers.PackersProjectTeamPopAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PakersProjectTeamPopWindow extends BasePopWindow {

    private PackersProjectTeamPopAdapter cidTypeAdapter;
    private PackersProjectTeamPopAdapter typeAdapter;
    private PackersProjectTeamPopAdapter scaleAdapter;
    private PackersProjectTeamPopAdapter teamAdapter;
    private PackersProjectTeamPopAdapter statusAdapter;
    private List<PickSelectEntity> cidList = new ArrayList<>();
    private List<PickSelectEntity> typeList = new ArrayList<>();
    private List<PickSelectEntity> scalList = new ArrayList<>();
    private List<PickSelectEntity> teamList = new ArrayList<>();
    private List<PickSelectEntity> statusList = new ArrayList<>();

    private String cidNo = "";
    private String typeNo = "";
    private String scalNo = "";
    private String scalName="";
    private String teamNo = "";
    private String statusNo = "";
    private String currentCidNo = "";

    private ProjectTeamPopListener listener;

    private MaxRecyclerView typeListView;

    public PakersProjectTeamPopWindow(Context context) {
        super(context,DIR_RIGHT);
        setSoftInputMode
                (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN|
                        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    protected int popLayout() {
        return R.layout.pop_window_project_team_filter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        MaxRecyclerView cidListView = holder.getPop().findViewById(R.id.cid_list);
        typeListView = holder.getPop().findViewById(R.id.type_list);
        MaxRecyclerView scalListView = holder.getPop().findViewById(R.id.scal_list);
        MaxRecyclerView teamListView = holder.getPop().findViewById(R.id.team_list);
        MaxRecyclerView statusListView = holder.getPop().findViewById(R.id.status_list);
        TextView tv_reset = holder.getPop().findViewById(R.id.reset_step);
        TextView tv_sure = holder.getPop().findViewById(R.id.sure_step);

        cidTypeAdapter = new PackersProjectTeamPopAdapter(mContext,null);
        cidListView.setLayoutManager(new GridLayoutManager(mContext,3));
        cidListView.setAdapter(cidTypeAdapter);
        cidTypeAdapter.setItemSelect(true);
        cidTypeAdapter.setDefItem(0);
        cidTypeAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                getFilterTypeData1(pickSelectEntity.getNo());
                cidNo = pickSelectEntity.getNo();

                if(!currentCidNo.equals(cidNo)){
                    typeAdapter.resetCheckDatas();
                    typeAdapter.setDefItem(-1);
                    scaleAdapter.resetCheckDatas();
                    scaleAdapter.setDefItem(-1);
                    teamAdapter.resetCheckDatas();
                    teamAdapter.setDefItem(-1);
                    statusAdapter.resetCheckDatas();
                    statusAdapter.setDefItem(-1);

                    typeNo = "";
                    scalNo = "";
                    scalName="";
                    teamNo = "";
                    statusNo = "";
                }

//                Log.v("filter_cid",pickSelectEntity.getProjectscale());
            }
        });

        typeAdapter = new PackersProjectTeamPopAdapter(mContext,null);
        typeListView.setLayoutManager(new GridLayoutManager(mContext,3));
        typeListView.setAdapter(typeAdapter);
        typeAdapter.setItemSelect(true);
        typeAdapter.setDefItem(-1);
        typeAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
//                Log.v("filter_type",pickSelectEntity.getProjectscale());
                typeNo = pickSelectEntity.getNo();
            }
        });

        scaleAdapter = new PackersProjectTeamPopAdapter(mContext,null);
        scalListView.setLayoutManager(new GridLayoutManager(mContext,3));
        scalListView.setAdapter(scaleAdapter);
        scaleAdapter.setItemSelect(true);
        scaleAdapter.setDefItem(-1);
        scaleAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
//                Log.v("filter_scale",pickSelectEntity.getProjectscale());
                scalNo = pickSelectEntity.getNo();
                if(pickSelectEntity.getProjectscale().equals("全部")) {
                    scalName = "";
                }else {
                    scalName = pickSelectEntity.getProjectscale();
                }
            }
        });

        teamAdapter = new PackersProjectTeamPopAdapter(mContext,null);
        teamListView.setLayoutManager(new GridLayoutManager(mContext,3));
        teamListView.setAdapter(teamAdapter);
        teamAdapter.setItemSelect(true);
        teamAdapter.setDefItem(-1);
        teamAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
//                Log.v("filter_team",pickSelectEntity.getProjectscale());
                teamNo = pickSelectEntity.getNo();
            }
        });

        statusAdapter = new PackersProjectTeamPopAdapter(mContext,null);
        statusListView.setLayoutManager(new GridLayoutManager(mContext,3));
        statusListView.setAdapter(statusAdapter);
        statusAdapter.setItemSelect(true);
        statusAdapter.setDefItem(-1);
        statusAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
//                Log.v("filter_status",pickSelectEntity.getProjectscale());
                statusNo = pickSelectEntity.getNo();
            }
        });

        //重置
        tv_reset.setOnClickListener(view -> {
            cidTypeAdapter.resetCheckDatas();
            cidTypeAdapter.setDefItem(0);
            typeAdapter.resetCheckDatas();
            typeAdapter.setDefItem(-1);
            scaleAdapter.resetCheckDatas();
            scaleAdapter.setDefItem(-1);
            teamAdapter.resetCheckDatas();
            teamAdapter.setDefItem(-1);
            statusAdapter.resetCheckDatas();
            statusAdapter.setDefItem(-1);

            cidNo = cidList.get(0).getNo();
            typeNo = "";
            scalNo = "";
            scalName="";
            teamNo = "";
            statusNo = "";
            if(null!=listener){
                listener.setResetResultListener(cidNo,typeNo,scalNo,teamNo,statusNo,scalName);
            }
            dissMiss();
        });
        //确定
        tv_sure.setOnClickListener(view -> {
            if(null!=listener){
                listener.setResultListener(cidNo,typeNo,scalNo,teamNo,statusNo,scalName);
            }
            dissMiss();
        });
    }

    public void setData(List<PickSelectEntity> cidList,List<PickSelectEntity> typeList,List<PickSelectEntity> scalList,List<PickSelectEntity> teamList,List<PickSelectEntity> statusList){
        this.cidList = cidList;
        this.typeList = typeList;
        this.scalList = scalList;
        this.teamList = teamList;
        this.statusList = statusList;

        cidTypeAdapter.setDatas(cidList);

        if(!TextUtils.isEmpty(cidNo)){
            for(int i=0;i< cidList.size();i++){
                if(cidNo.equals(cidList.get(i).getNo())){
                    cidTypeAdapter.setDefItem(i);
                    getFilterTypeData(cidList.get(i).getNo());
                    break;
                }
            }
        }

        if(null==typeList || typeList.size()==0){
            typeListView.setVisibility(View.GONE);
        }else{
            typeListView.setVisibility(View.VISIBLE);
            typeAdapter.setDatas(typeList);
            typeAdapter.setDefItem(-1);
            if(!TextUtils.isEmpty(typeNo)){
                for(int i=0;i< typeList.size();i++){
                    if(typeNo.equals(typeList.get(i).getNo())){
                        typeAdapter.setDefItem(i);
                        break;
                    }
                }
            }
        }

        scaleAdapter.setDatas(scalList);
        scaleAdapter.setDefItem(-1);
        if(!TextUtils.isEmpty(scalNo)){
            for(int i=0;i< scalList.size();i++){
                if(scalNo.equals(scalList.get(i).getNo())){
                    scaleAdapter.setDefItem(i);
                    break;
                }
            }
        }

        teamAdapter.setDatas(teamList);
        teamAdapter.setDefItem(-1);
        if(!TextUtils.isEmpty(teamNo)){
            for(int i=0;i< teamList.size();i++){
                if(teamNo.equals(teamList.get(i).getNo())){
                    teamAdapter.setDefItem(i);
                    break;
                }
            }
        }

        statusAdapter.setDatas(statusList);
        statusAdapter.setDefItem(-1);
        if(!TextUtils.isEmpty(statusNo)){
            for(int i=0;i< statusList.size();i++){
                if(statusNo.equals(statusList.get(i).getNo())){
                    statusAdapter.setDefItem(i);
                    break;
                }
            }
        }
    }

    public void setCid(String id){
        cidNo = id;
        currentCidNo = id;
    }
    public void setType(String id){
        typeNo = id;
    }
    public void setScale(String id){
        scalNo = id;
    }
    public void setTeam(String id){
        teamNo = id;
    }
    public void setStatus(String id){
        statusNo = id;
    }

    public void getFilterTypeData(String id) {
        APIManager.getInstance().getPackerAPI().getSecondCategory(id).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    typeList.clear();
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("0");
                    entity.setProjectscale("全部");
                    typeList.add(0,entity);
                    if(null!=response.body().getData()){
                        typeList.addAll(response.body().getData());
                    }

                    if(null==typeList || typeList.size() == 0){
                        typeListView.setVisibility(View.GONE);
                    }else{
                        typeListView.setVisibility(View.VISIBLE);
                        typeAdapter.setDatas(typeList);
                        if(!TextUtils.isEmpty(typeNo)){
                            for(int i=0;i< typeList.size();i++){
                                if(typeNo.equals(typeList.get(i).getNo())){
                                    typeAdapter.setDefItem(i);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    public void getFilterTypeData1(String id) {
        APIManager.getInstance().getPackerAPI().getSecondCategory(id).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    typeList.clear();
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("0");
                    entity.setProjectscale("全部");
                    typeList.add(0,entity);
                    if(null!=response.body().getData()){
                        typeList.addAll(response.body().getData());
                    }

                    if(null==typeList || typeList.size() == 0){
                        typeListView.setVisibility(View.GONE);
                    }else{
                        typeListView.setVisibility(View.VISIBLE);
                        typeAdapter.setDatas(typeList);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    public interface ProjectTeamPopListener{
        void setResultListener(String cid,String type,String scale,String team,String status,String name);

        void setResetResultListener(String cid,String type,String scale,String team,String status,String name);
    }

    public void setResuleListener(ProjectTeamPopListener l){
        this.listener = l;
    }
}
